import { assign } from 'lodash';
const reg = /([上下]行)?\(\S+\)/


export default {
  // defaults 返回一个对象，用于初始化页面state
  defaults(props) {
    return {
      empty: props.empty || true,
      keyword: '',
      keywords: [],
      city: '杭州市',
      show: false,
      param: {
        customer: '09eb9edad501e8ed917b9dbb0fb4ed66',
        encode: 'utf-8',
        json: '0'
      },
      stations: []
    };
  },
  updateState(ctx, data) {
    ctx.setState(data);
  },

  // awareOf 是 LogicRender 检测到需要执行action的时候带出来的
  // awareOf 是上一次的“当前”数据，可以从 getState 中获取最新的
  async search(ctx/* , awareOf*/) {
    const { setState, getState, fn: { message, DB } } = ctx;
    let params = { keyword: getState().keyword, city: getState().city };
    params = assign({}, params, getState().param);
    let state = {};
    try {
      DB.Bus.line.config.query = params;
      const Bus = await DB.Bus.line();
      const routes = Bus.result[0].routes[0].item;
      const empty = routes == undefined || !routes.length;
      if (empty) {
        !params.keyword ? '' : message.info(`${params.keyword}查无数据！`);
        state = assign({}, { empty, data: [], show: false });
      } else {
        let data_1 = routes,
          data_one = data_1.map((item, key) => item.name[0]._text.replace(reg, '')),
          data_2 = [],
          data_two = [];
        for (let i = 0; i < data_one.length; i++) {
          if (data_one[i] == data_one[i + 1]) {
            data_two.push(data_one[i]);
            data_2.push([data_1[i].name[0]._text, data_1[i + 1].name[0]._text]);
            i++;
          } else {
            data_two.push(data_one[i]);
            data_2.push([data_1[i].name[0]._text]);
          }
        }
        message.success(`${params.keyword}请求成功！`);
        state = assign({}, { empty, data: data_two, realData: data_2, show: false });
      }

    } catch (e) {
      message.error(`${params.keyword}请求出错啦！`);
      state = { data: [], empty: false, show: false };
    }
    setState(state);
  },

  async searchRoutes(ctx) {
    const { setState, getState, fn: { message, DB } } = ctx;
    let params = { city: getState().city };
    params = assign({}, params, getState().param);
    let keywords = getState().keywords,
    keyword = getState().keyword;
    let state = {};
    try {
      let routes = {}, stations = [], empty = false;
      for (let i = 0; i < keywords.length; i++) {
        params.keyword = keywords[i];
        DB.Bus.routes.config.query = params;
        routes = await DB.Bus.routes();
        let _stations = routes.result[0].route[0].stations[0].item.map((item, key)=> item.name[0]._text);
        stations.push(_stations);
        empty = _stations == undefined || !_stations.length;
      }
      if (empty) {
        message.info(`${keywords[0].replace(reg, '')}  查无数据！`);
        state = assign({}, { empty, stations, show: false });
      } else {
        message.success(`${keywords[0].replace(reg, '')}  请求成功！`);
        state = assign({}, { empty, stations, show: true });
      }

    } catch (e) {
      !keywords.length ? '' : message.error(`${keywords[0].replace(reg, '')}  请求出错啦！`);
      state = { Bus: [], empty: false, show: false };
    }
    setState(state);
  },
};
